package com.wz.leetcode.linkedlist;

import com.wz.leetcode.linkedlist.ListNode;

public class RemoveNthNode_19 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if ( n <= 0 || head == null) return head;
        ListNode dummy = new ListNode(-1, head);
        ListNode fast = dummy, slow = dummy;
        /*(沿着链表)移动了k次*/
        for(int count = 1; count <= n; count++) {
            if (fast != null) {
                fast = fast.next;
            } else {
                return head;
            }
        }

        while(fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }

}
